package com.atguigu.oss.controller;

import com.atguigu.commonutils.R;
import com.atguigu.oss.entity.UploadFileResponse;
import com.atguigu.oss.service.FileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;

@Api(description="本地文件管理")
@RestController
@RequestMapping("/eduoss/filetest")
public class FileController {

    @Autowired
    FileService service;

    @PostMapping("uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file){

        String filename = service.storeFile(file);
        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(filename)
                .toUriString();

        return R.ok().data("url",fileDownloadUri);
    }

}
